<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
/**
 *
 */
class Set extends Controller
{
    //保存合作信息
    function saveCooperation($name,$gname,$content,$tel,$ts,$sex,$mode)
    {
        if(!$name||!$gname||!$content||!$tel||!$ts||!$sex){
            return json_encode(0);
        }

        $data=[
            'name'=>$name,  //名字
            'sex'=>$sex,    //性别
            'fs'=>$ts,      //联系方式名称：手机、微信
            'contact'=>$tel,//号码
            'company'=>$gname,//公司名称
            'content'=>$content, //合作内容
            'mode'=>$mode,     //合作方式
            'status'=>0     //未解决
        ];
        model('Set')->saveCooperation($data);
        return json_encode(1);
    }


    //保存意见与建议
    function saveOpinion($name, $content, $tel, $ts, $sex,$problem)
    {
        if(!$name||!$content||!$tel||!$ts||!$sex){
            return json_encode(0);
        }

        $data = [
            'name'=>$name,  //联系人
            'sex'=>$sex,    //性别
            'fs'=>$ts,      //联系方式：微信1，手机2
            'contact'=>$tel,//联系号码
            'comment'=>$content,//意见内容
            'status'=>0,        //未解决
            'problem'=>$problem   //问题分类
        ];
        model('Set')->saveOpinion($data);
        return json_encode(1);

    }


    function index($code){
        $data = file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid=wx989c952dbfa8ee8c&secret=09aa68e3a0ba23400ffc2d89f84ca213&js_code=".$code."&grant_type=authorization_code");
        $data = json_decode($data,true);
        return $data["openid"];

    }
    function text($code){
        $data = getAccessToken($code);
        print_r($data);
        exit();
    }
     //保存用户信息
    function saveUserMessage()
    {
        $openID = $_GET['openID'];
        $data['openID'] =$openID;
        $open = db('user')->where('openID',$openID)->find();
        if($open){
            db('user')->where('openID',$openID)->update(['logintime'=>time()]);
            db('user')->where('openID',$openID)->setInc('status');
            return json_encode(1);
        }
        // $nickName, $gender, $city, $province, $country, $avatarUrl,
        if(isset($_GET['nickName']))$data['nickName']=$_GET['nickName'];
        if(isset($_GET['gender']))$data['gender']=$_GET['gender'];
        if(isset($_GET['city']))$data['city']=$_GET['city'];
        if(isset($_GET['province']))$data['province']=$_GET['province'];
        if(isset($_GET['country']))$data['country']=$_GET['country'];
        if(isset($_GET['avatarUrl']))$data['avatarUrl']=$_GET['avatarUrl'];
        $data['logintime'] = time();
        if(!$data['openID']||!$data['avatarUrl']||!$data['nickName']){
            return json_encode(0);
        }
        model('Set')->saveUserMessage($data);
        return json_encode(1);
    }
    // 搜索词条新增热门搜索
    function save_ct_hot_search($bangdan_id,$teacase_id,$teaarea_id){
        //没提交 不执行
         if(!$bangdan_id||!$teacase_id||!$teaarea_id){
            return null;
        }
        $data = ['bangdan_id'=>$bangdan_id,'teacase_id'=>$teacase_id,'teaarea_id'=>$teaarea_id];
        // 搜索是否存在热门搜索
        mp($data);
        $res = db('Hot_search')->where($data)->count();
        if($res>0){
            db('Hot_search')->where($data)->setInc('frequency');
        }else{
            db('Hot_search')->insert($data);
        }
    }

    // 访问轨迹的方法
    function save_user_url($bangdan_id,$teacase_id,$teaarea_id,$id,$openid){
    // 访问时间，访问人，访问的词条路径

    }

    // 修改词条提交审核表里
     function updata_ct_name($exist_id,$name,$sex,$contact,$bangdan_id,$teacase_id,$teaarea_id,$detailintro,$phone="",$wechat=""){
        // return 0 不存在榜单id失败
        $data['updatetime'] = time();
        $data['exist_id'] = $exist_id;
        $data['name'] = $name;
        $data['phone'] = $phone;
        $data['wechat'] = $wechat;
        $data['sex'] = $sex;
        $data['contact'] = $contact;//联系人
        $data['bangdan_id'] = $bangdan_id;
        $data['teacase_id'] = $teacase_id;
        $data['teaarea_id'] = $teaarea_id;
        $data['detailintro'] = $detailintro;
        $data['mark'] = 0;
        if($data['bangdan_id']==1){
            if(isset($_GET['briefintro'])){
                $data['briefintro'] = $_GET['briefintro'];
            }
        }
        if($data['bangdan_id']==2){
            if(isset($_GET['relatedwords'])){
                $data['relatedwords']= $_GET['relatedwords'];
            }
            if(isset($_GET['sale'])){
                $data['sale'] = $_GET['sale'];
            }
            if(isset($_GET['company'])){
                $data['company'] = $_GET['company'];
            }
            if(isset($_GET['pnumber'])){
                $data['pnumber'] = $_GET['pnumber'];
            }
        }
        if($data['bangdan_id']==3){
            if(isset($_GET['producer'])){
                $data['producer'] = $_GET['producer'];
            }
            if(isset($_GET['zs'])){
                $data['zs'] = $_GET['zs'];
            }
            if(isset($_GET['gs'])){
                $data['gs'] = $_GET['gs'];
            }
            if(isset($_GET['ct'])){
                $data['ct'] = $_GET['ct'];
            }
            if(isset($_GET['ds'])){
                $data['ds'] = $_GET['ds'];
            }
            if(isset($_GET['orther'])){
                $data['orther'] = $_GET['orther'];
            }
            if(isset($_GET['pirce'])){
                $data['pirce'] = $_GET['pirce'];
            }
        }
        model('Set')->UpdateReview($data);
        return 1;
    }
    // 新增词条
    function save_ct_name($name,$sex,$contact,$bangdan_id,$teacase_id,$teaarea_id,$detailintro,$phone="",$wechat=""){
        // return 0 不存在榜单id失败
        $data['addtime'] = time();
        $data['updatetime'] = time();
        $data['name'] = $name;
        $data['phone'] = $phone;
        $data['wechat'] = $wechat;
        $data['sex'] = $sex;
        $data['contact'] = $contact;//联系人
        $data['bangdan_id'] = $bangdan_id;
        $data['teacase_id'] = $teacase_id;
        $data['teaarea_id'] = $teaarea_id;
        $data['detailintro'] = $detailintro;
        if($data['bangdan_id']==1){
            if(isset($_GET['briefintro'])){
                $data['briefintro'] = $_GET['briefintro'];
            }
        }
        if($data['bangdan_id']==2){
            if(isset($_GET['relatedwords'])){
                $data['relatedwords']= $_GET['relatedwords'];
            }
            if(isset($_GET['sale'])){
                $data['sale'] = $_GET['sale'];
            }
            if(isset($_GET['company'])){
                $data['company'] = $_GET['company'];
            }
            if(isset($_GET['pnumber'])){
                $data['pnumber'] = $_GET['pnumber'];
            }
        }
        if($data['bangdan_id']==3){
            if(isset($_GET['producer'])){
                $data['producer'] = $_GET['producer'];
            }
            if(isset($_GET['zs'])){
                $data['zs'] = $_GET['zs'];
            }
            if(isset($_GET['gs'])){
                $data['gs'] = $_GET['gs'];
            }
            if(isset($_GET['ct'])){
                $data['ct'] = $_GET['ct'];
            }
            if(isset($_GET['ds'])){
                $data['ds'] = $_GET['ds'];
            }
            if(isset($_GET['orther'])){
                $data['orther'] = $_GET['orther'];
            }
            if(isset($_GET['price'])){
                $data['price'] = $_GET['price'];
            }
        }
        model('Set')->insertReview($data);
        return 1;
    }


}

?>